﻿requirejs.config({
    baseUrl: ".",
    paths: {
    	zepto:"javascripts/zepto.min",
    	dingtalk:"javascripts/dingtalk",
        vue: "components/vue/vue.min",
        axios: "components/axios/axios.min",
        MINT: "components/mint-ui/index",
        moment: "components/moment/moment.min",
        search: "resources/js/search",
        datetime: "resources/js/datetime",
        dictionary: "resources/js/dictionary",
        common: "resources/js/common",
        wx: "http://res.wx.qq.com/open/js/jweixin-1.2.0",
        jssdk: "resources/js/jssdk"
    },
    shim: {
        "MINT": {
            deps: ["vue"],
            exports: "MINT"
        },
        "wx": {
            exports: "wx"
        }
    }
});

require(["zepto","dingtalk","vue", "axios", "common", "MINT", "moment", "search", "datetime", "dictionary", "jssdk"], function (zepto,dingtalk,Vue, axios, common, MINT, moment, search, datetime, dictionary, jssdk) {

    Vue.component(MINT.Popup.name, MINT.Popup);
    Vue.component(MINT.Toast.name, MINT.Toast);

    window.MINT = MINT;

    var vm = new Vue({
        el: "#view",
        data: {
            detail: {},
            others: {},
            lines: [],

            identifier: {
                id: "",
                name: ""
            },
            executer: {
                id: "",
                name: ""
            },

            nextAssigner: "",
            suggestion: "",
            commons: "",

            submituserlisturl: "",
            nextAssignerSearch_: {
                id: "",
                name: ""
            },
            submitVisible: false,
            bpm_users: [],
            bpm_commons: [],
            submitLine: {}
        },
        computed: {
            nextAssignerSearch: {
                get: function () {
                    return this.nextAssignerSearch_;
                },
                set: function (val) {
                    this.nextAssignerSearch_ = val;
                    this.nextAssigner = this.nextAssignerSearch_.id;
                }
            }
        },
        mounted: function () {
            var self = this;
            var id = common.getParam("id");
            axios.get("/change/detail?id=" + id)
                .then(function (response) {
                    if (!response.data.data.executeTime) {
                        response.data.data.executeTime = null;
                    }
                    self.detail = response.data.data;
                    self.others = response.data.others;
                    self.lines = response.data.lines;

                    if (self.detail.status == "变更实施") {
                        axios.get("/common/userInfo")
                            .then(function (userInfo) {
                                self.executer.id = userInfo.data.name;
                                self.executer.name = userInfo.data.fullname;
                            });
                    }
                });
        },
        methods: {
            createData: function () {
                var data = {
                    id: this.detail.id
                }
                if (this.detail.status == "制定变更方案") {
                    data.riskEvaluate = this.detail.riskEvaluate;
                    data.effectAnalysis = this.detail.effectAnalysis;
                    data.changePlan = this.detail.changePlan;
                }
                if (this.detail.status == "变更测试") {
                    data.publishDescription = this.detail.publishDescription;
                }
                if (this.detail.status == "变更实施") {
                    data.identifier = this.identifier.id;
                    data.identifierName = this.identifier.name;
                    data.effectAnalysis = this.detail.effectAnalysis;
                    data.executeTime = this.detail.executeTime;
                    data.executer = this.executer.id;
                    data.executerName = this.executer.name;
                    data.executeResult = this.detail.executeResult;
                }
                if (this.detail.status == "变更回顾") {
                    data.evaluationResult = this.detail.evaluationResult;
                }
                return data;
            },
            submit: function (line) {
                if (this.detail.status == "制定变更方案") {
                    if (common.isEmpty(this.detail.riskEvaluate)) {
                        MINT.Toast("请输入风险评估");
                        return;
                    }
                    if (common.isEmpty(this.detail.effectAnalysis)) {
                        MINT.Toast("请输入影响分析");
                        return;
                    }
                    if (common.isEmpty(this.detail.changePlan)) {
                        MINT.Toast("请输入变更方案");
                        return;
                    }
                }
                if (this.detail.status == "变更实施") {
                    if (common.isEmpty(this.identifier.id)) {
                        MINT.Toast("请输入验证人");
                        return;
                    }
                }
                if (this.detail.status == "变更回顾") {
                    if (common.isEmpty(this.detail.evaluationResult)) {
                        MINT.Toast("请输入评估结果");
                        return;
                    }
                }

                this.submitLine = line;
                var self = this;

                self.others.longitude = jssdk.location().longitude;
                self.others.latitude = jssdk.location().latitude;

                if (this.submitLine.processMode == 2 || this.submitLine.commitType != 0 || this.submitLine.areOpenCommentWindow) {
                    if (this.submitLine.processMode == 2 || (this.submitLine.processMode == 0 && this.submitLine.commitType == 1)) {
                        axios.get("/common/submitUser?key=changeProcess&processId=" + this.submitLine.processInstanceId + "&nextTaskId=" + this.submitLine.nextTaskId + "&businessKey=" + this.detail.id + "&processMode=" + this.submitLine.processMode)
                            .then(function (response) {
                                self.bpm_users = response.data;
                            });
                    }
                    if (this.submitLine.processMode == 0 && this.submitLine.commitType == 2) {
                        self.submituserlisturl = "/common/submitUserSelect?key=changeProcess&processId=" + this.submitLine.processInstanceId + "&nextTaskId=" + this.submitLine.nextTaskId + "&businessKey=" + this.detail.id + "&processMode=" + this.submitLine.processMode + "&department_path=";
                    }

                    axios.get("/common/dict?typeCode=BPM_COMMONS")
                        .then(function (response) {
                            self.bpm_commons = response.data;
                        });

                    this.submitVisible = true;
                } else {
                    var data = {
                        data: this.createData(),
                        line: this.submitLine,
                        images: this.images,
                        others: this.others
                    };
                    common.setColumn(data);

                    axios.post("/change/submit", data)
                        .then(function (response) {
                            alert(response.data.message);
                            common.goback();
                        });
                }
            },
            popupSubmit: function () {
                if ((this.submitLine.processMode == 2
                    || (this.submitLine.processMode == 0 && this.submitLine.commitType == 1)
                    || (this.submitLine.processMode == 0 && this.submitLine.commitType == 2))
                    && common.isEmpty(this.nextAssigner)) {
                    MINT.Toast({ message: "请输入下一步处理人", position: "middle" });
                    return;
                }

                var self = this;

                var data = {
                    data: this.createData(),
                    nextAssigner: this.nextAssigner,
                    suggestion: this.suggestion,
                    line: this.submitLine,
                    others: this.others
                };
                common.setColumn(data);

                axios.post("/change/submit", data)
                    .then(function (response) {
                        alert(response.data.message);
                        common.goback();
                    });
            },
            cancelSubmit: function () {
                this.submitLine = {};
                this.submitVisible = false;
            }
        }
    });
});